
/**
 * jQuery NC plugin file.
 *
 * @author Chuncheng Li <spricity@gmail.com>
 * @link http://www.diggbag.com/
 * @copyright Copyright &copy; 2010 Yii Software LLC
 * @license http://www.diggbag.com/license/
 * @version $Id: nc.news.js 1892 2010-10-13 19:49:31Z spricity $
 */

;(function($){

$.fn.nc = {
	version : '0.1',
	
	createNews : function(url){
		alert(url);
	}
};
$.spricity = function(){
	alert('spricity');
}
})(jQuery);

function nc_ajax(url, data, fun) {
	var d = eval(data);
	$.post(
		url,d,function(data){
			var json=$.parseJSON(data);
			fun=eval(fun);
			try{fun(json);}catch(e){};
		}
	);
}

function crawl(){
	//前端验证
	var Links_link_url = $("#Links_link_url").val();
	var test = /^(http|https):\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;
	var test2 = /^[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;
	if(!Links_link_url)return false;
	else if(!test.test(Links_link_url) && !test2.test(Links_link_url))return false;
	addError('请稍后...');
	$("#Links_link_url").attr('disabled',true);
	$("#ajaxButton").attr('disabled',true);
	$.post('/links/curl',{'ajax':'links-form','Links[link_url]':encodeURI(Links_link_url)},function(json){
		var f=1;
		try{var j=$.parseJSON(json);f = j.flag;}catch(e){f=true;}
		if(f){
			try{$("#links-form-box").remove();closeError();}catch(e){}
			$.fancybox({
			//'width'				: '75%',
			//'height'			: '75%',
			'autoScale'			: true,
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'type'				: 'html',
			'scrolling'			: 'no',
			'hideOnOverlayClick': false,
			//'centerOnScroll'	: true,
			'content'			: json,
			'onClosed'			: function(){
				$("#Links_link_url").attr('disabled',false);
				$("#ajaxButton").attr('disabled',false);
			}
			});//.trigger('click.fb');
		}else{
			addError(j.info);
			$("#Links_link_url").attr('disabled',false);
			$("#ajaxButton").attr('disabled',false);
		}
	});
}
$(function(){
		$('.list-news-item').ncItemHover();//整块hover
		$(".ding").ncDiggClick();//顶
		$(".cai").ncTrashClick();//踩
		//$(".nc-favor").ncFavoer();//头像hover
		$(".nc-favor").ncTips();//头像hover
		$(".upNews").upperHover();//上游列表hover
		$(".do").doUppper();//上游列表hover
		$(".doit").doit();//添加和取消上游
		$(".tips").ncTips();
		$(".jsin").inputText();//绑定所有的输入框
		$(".edit_but").edit();
		$("a#tag_a").fancybox({
			'width':'50%',
			'autoScale'			: false,
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'title':'增加标签',
			'titleShow':true,
			'titlePosition':'inside'
		});
		$(".tagbtn").tagbtn("taginput");
});

$.fn.tagbtn = function(inputid){
	var inputhdl = $("#"+inputid);
	var tagStr = new Array();
	return this.each(function(){
		var $this = $(this);
		$this.toggle(function(){
			var tagname = $this.text();
			var oldTagStr = inputhdl.val();
				var oldTags = oldTagStr.ncSplit(' ');//字符串转换成数组
				if(tagStr.length != oldTags.length){
					tagStr.length=0; //清空数组
					tagStr = oldTags.slice(0); //复制数组
				}
				tagStr.push(tagname);
				var nowTags = tagStr.join(' ')+' '; //数组转换成字符串
				inputhdl.val(nowTags).focus();
				$this.addClass('taged').removeClass('tagging');
		},function(){
			var tagname = $this.text();
			tagStr.removeItem(tagname);//移除指定的名字的数组
			var nowTags = tagStr.join(' ')+' ';//数组转换成字符串
			inputhdl.val(nowTags).focus();
			$this.addClass('tagging').removeClass('taged');
		});
	});
}

//新闻 hover
$.fn.ncItemHover = function(){
	return this.each(function(){
		var t = $(this);
		t.hover(function(){
			this.style.background='#EAF4FC';
			this.style.borderColor='#A2C3E7';
			t.find('.digg-hover')[0].style.background = 'url("/images/ding_hover.png") no-repeat scroll 0 0 transparent';
			t.find('.trash-hover')[0].style.background = 'url("../images/cai_hover.png") no-repeat scroll 0 0 transparent';
		},function(){
			this.style.background='#fff';
			this.style.borderColor='#fff';
			t.find('.digg-hover')[0].style.background = 'url("/images/ding.png") no-repeat scroll 0 0 transparent';
			t.find('.trash-hover')[0].style.background = 'url("/images/cai.png") no-repeat scroll 0 0 transparent';
		});
	});
}
//顶
$.fn.ncDiggClick = function(){
	return this.each(function(){
		$(this).bindAfterLogin('click',function(){
			var $this = $(this);
			$ajax('/actions/digg',{'dc':this.id},function(json){
				$this[0].style.background = 'url("/images/ding.png") no-repeat scroll 0 0 transparent';
				$this.text(json.info).unbind('click').removeClass('digg-hover');
				$this.parent().find('.trash-hover').unbind().css('background','url("/images/cai.png") no-repeat scroll 0 0 transparent').removeClass('trash-hover');
			});
		});
	});
}
//踩
$.fn.ncTrashClick = function(){
	return this.each(function(){
		$(this).bindAfterLogin('click',function(){
			var $this = $(this);
			$ajax('/actions/trash',{'tr':this.id},function(json){
				$this[0].style.background = 'url("/images/cai.png") no-repeat scroll 0 0 transparent';
				$this.text(json.info).unbind('click').removeClass('trash-hover');
				$this.parent().find('.digg-hover').unbind().css('background','url("/images/ding.png") no-repeat scroll 0 0 transparent').removeClass('digg-hover');
			});
		});
	});
}
//站内其他地方添加和取消上游
$.fn.doit = function(){
	return this.each(function(){
		$(this).bindAfterLogin('click',function(){
			var $this = $(this);
			var id = this.id;
			$this.ncError().trigger('nc.errorclose');
			$ajax('/upper/doit',{'do':id},function(json){
				if(json.flag){
					switch(json.info){
						case 'ad':
							var re = /ad/ig;
							var h = id.replace(re,'de');
							$this.removeClass('add-upper').addClass('del-upper');
							$this.attr('id',h);
							break;
						case 'de':
							var re = /de/ig;
							var h = id.replace(re,'ad');
							$this.removeClass('del-upper').addClass('add-upper');
							$this.attr('id',h);
						break;
					}
				}else
					$this.ncError().trigger('nc.error',json.info);
			});
		});
	});
}

function triggerImgeHover(){
	//$(".nc-favor").ncFavoer();//头像hover
	$(".nc-favor").ncTips();//头像hover
}
$.fn.upperHover = function(){
	return this.each(function(){
		var $this = $(this);
		$this.hover(function(){
			$this.find("img.do").css('display','inline');
		},function(){
			$this.find("img.do").hide();
		});
	});
}
//首页添加和取消上游
$.fn.doUppper = function(){
	return this.each(function(){
		var $this = $(this);
		$this.bindAfterLogin('click',function(){
		 var id = this.id;
			$this.ncError().trigger('nc.errorclose');
			$ajax('/upper/doit',{'do':id},function(json){
				if(json.flag){
					$("#list").empty();
					$("#shares").empty();
					$("#load1").show();
					$("#load2").show();
					$.get('/news/refresh',function(dat){
						$("#load1").hide();
						$("#list").html(dat);
						$('.list-news-item').ncItemHover();//整块hover
						$(".ding").ncDiggClick();//顶
						$(".cai").ncTrashClick();//踩
					});
					$.get('/news/refresh/f/upper',function(dat){
						$("#load2").hide();
						$("#shares").html(dat);
						$(".upNews").upperHover();//上游列表hover
						$(".do").doUppper();//上游列表hover
						$(".tips").ncTips();
					});
						/*
					switch(json.info){
						case 'ad':
							var re = /ad/ig;
							var h = $(id.replace(re,'#ups'));
							var clone = h.clone();
							clone.find("img.do").remove();
							h.fadeOut('fast',function(){
								h.remove();
								$("#shares").prepend(clone);
							});
							break;
						case 'de':
							var re = /de/ig;
							var h = $(id.replace(re,'#ups'));
							h.fadeOut('fast',function(){
								$(this).remove();
							});
							$("#list").empty();
							$("#load1").show();
							$("#load2").show();
							$.get('/news/refresh',function(dat){
								$("#load1").hide();
								$("#list").html(dat);
								$('.list-news-item').ncItemHover();//整块hover
								$(".ding").ncDiggClick();//顶
								$(".cai").ncTrashClick();//踩
							});
							$.get('/news/refresh/f/upper',function(dat){
								$("#load2").hide();
								$("#shares").html(dat);
								$(".upNews").upperHover();//上游列表hover
								$(".do").doUppper();//上游列表hover
								$(".tips").ncTips();
							});
						break;
					}
							*/
				}else $this.ncError().trigger('nc.error',json.info);
			});
		});
	});
}

//删除兴趣
$.fn.delFavor = function(){
	this.each(function(){
		var $this=$(this);
		var tag = this.rel;
		$(this).bindAfterLogin('click',function(){
			$this.unbind();
			$this.ncError().trigger('nc.errorclose');
			$ajax('/favor/delete',{'tag':encodeURI(tag)},function(json){
				if(json.flag){
					$this.parents('li').fadeOut('fast',function(){$(this).remove();});
				}else {
					$this.ncError().trigger('nc.error',json.info);
					$this.delFavor();
				}
			});
		});
	});
}
$(function(){
	$('.han-del-tag').delFavor();
	$("#favor-submit").bindAfterLogin("click",function(){
		var $this=$(this);
		$this.ncError().trigger('nc.errorclose');
		$this.$send({"id":"favor-form","call":function(json){
			var tagName = $("#tag-name");
			if(json.flag){
				var html = '<li class="interest_li">'+
					'<span class="interest_one_tag"></span>'+
					'<span> '+tagName.val()+' </span>'+
					'<span><a href="javascript:" rel="'+tagName.val()+'" id="'+json.info+'han-del-tag"></a></span>'+
				'</li>';
					$("#favor-list").append(html).fadeIn('fast');
					tagName.val('我写我兴趣...');
					$("#"+json.info+'han-del-tag').delFavor();
					tagName.blur();
				}else{ $this.ncError().trigger('nc.error',json.info);tagName.val('我写我兴趣...');tagName.blur();}
		}});
	});
	$("#favor-register").bindAfterLogin("click",function(){
		var $this=$(this);
		$this.ncError().trigger('nc.errorclose');
		if($("#tag-name").val()=='我写我兴趣...')return false;
		$this.$send({"id":"favor-form","call":function(json){
			var tagName = $("#tag-name");
			if(json.flag){
				var html ='<span> '+tagName.val()+' </span>'
					$("#favor-list").append(html).fadeIn('fast');
					tagName.val('我写我兴趣...').css('color','#bbb');
					tagName.blur();
				}else{ $this.ncError().trigger('nc.error',json.info);tagName.val('我写我兴趣...');tagName.blur();}
		}});
	});
	$(".digg-favor").bindAfterLogin("click",function(){
		var $this = $(this);
		$this.ncError().trigger('nc.errorclose');
		var v=$this.text();
		$ajax("/favor/add",{'tag':$this.text()},function(json){
			var tagName = $("#tag-name");
			if(json.flag){
				var html ='<span class="span"> '+$this.text()+' </span>'
					$("#favor-list").append(html).fadeIn('fast');
					tagName.val('我写我兴趣...');
					tagName.blur();
				}else{ $this.ncError().trigger('nc.error',json.info);tagName.val('我写我兴趣...');tagName.blur();}
		});
	});
	//修改密码
	$("#setting-password").bind("click",function(){
			$(this).focus();
		$(this).ncError().trigger('nc.errorclose');
			$(this).attr('disabled',false);
		$(this).$send({"id":"setting-form","call":function(json){
			$(this).attr('disabled',true);
			if(json.flag){
				$.fancybox({
					'type':'html',
					'content':'保存成功'
				});
			}else{
				var message = '';
				for(i in json.info){
					message += json.info[i];
				}
				$(this).ncError().trigger('nc.error',message);
			}
		}});
		return false;
	});
});

$.fn.edit = function(){
	return this.each(function(){
		$(this).fancybox({
			'transitionIn'		: 'none',
			'transitionOut'		: 'none'
		});
	});
}
